import { Component, ViewChild, Injector, Output, EventEmitter, ElementRef } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import { TechRuleServiceProxy, TechRuleEditDto, ComboboxItemDto, LocationDto, LocationServiceProxy, CreateOrUpdateTechRuleInput, TechRuleInfoDto } from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';

import * as moment from 'moment';
import { finalize } from 'rxjs/operators';

@Component({
  selector: 'createOrEditTechRuleModal',
  templateUrl: './create-or-edit-techrule-modal.component.html'
})

export class CreateOrEditTechRuleModalComponent extends AppComponentBase {

  @ViewChild('createOrEditModal') modal: ModalDirective;
  @ViewChild('dueDatePicker') dueDatePicker: ElementRef;
  @ViewChild('areaCombobox') areaCombobox: ElementRef;
  @ViewChild('shelfCombobox') shelfCombobox: ElementRef;
  @ViewChild('positionCombobox') positionCombobox: ElementRef;
  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

  active = false;
  saving = false;

  techrule = new TechRuleEditDto();

  areas = new Array<LocationDto>();
  shelfs = new Array<LocationDto>();
  positions = new Array<LocationDto>();
  areaId = 0;
  shelfId = 0;
  positionId = 0;
  positionName;
  type: number;
  constructor(
    injector: Injector,
    private _techruleService: TechRuleServiceProxy,
    private _locationServiceProxy: LocationServiceProxy
  ) {
    super(injector);
  }

  show(id?: string, type?: number): void {
    this.active = true;
    this.type = type;
    this.areaId = 0;
    this.shelfId = 0;
    this.positionId = 0;
    this._locationServiceProxy.getAreas()
      .subscribe(result => {
        this.areas = result.items;
      });

    this._techruleService.getTechRuleForEdit(id).subscribe(result => {
      this.techrule = result.techRule;
      this.techrule.techRuleInfo = new TechRuleInfoDto();
      if (result.techRule.positionId) {
        this.positionId = result.techRule.positionId;
        this._locationServiceProxy.getPositionsById(this.techrule.positionId)
          .subscribe(result => {
            this.positionName = result;
          });
      }

      setTimeout(() => {
        $(this.areaCombobox.nativeElement).selectpicker('refresh');
        $(this.shelfCombobox.nativeElement).selectpicker('refresh');
        $(this.positionCombobox.nativeElement).selectpicker('refresh');
      }, 0);
      this.modal.show();
    });
  }

  loadShelfs() {
    this.shelfId = 0;
    this.positionId = 0;

    this._locationServiceProxy.getShelvesByArea(this.areaId)
      .subscribe(result => {
        this.shelfs = result.items;
        setTimeout(() => {
          $(this.shelfCombobox.nativeElement).selectpicker('refresh');
        }, 0);
      });
  }

  loadPositions() {
    this.positionId = 0;

    this._locationServiceProxy.getPositionsByShelf(this.shelfId)
      .subscribe(result => {
        this.positions = result.items;
        setTimeout(() => {
          $(this.positionCombobox.nativeElement).selectpicker('refresh');
        }, 0);
      });
  }

  save(): void {
    this.saving = true;
    if (this.techrule.id) {
      this.techrule.positionId = this.positionId;
      this._techruleService.createOrUpdateTechRule(this.techrule)
        .pipe(finalize(() => { this.saving = false; }))
        .subscribe(() => {
          this.notify.info(this.l('SavedSuccessfully'));
          this.close();
          this.modalSave.emit(null);
        });
    }
    else {
      var input = new CreateOrUpdateTechRuleInput();
      input.operation = this.type;
    //   input.techRule = this.techrule;
      this._techruleService.scanTechRule(input)
        .pipe(finalize(() => { this.saving = false; }))
        .subscribe(() => {
          this.notify.info(this.l('SavedSuccessfully'));
          this.close();
          this.modalSave.emit(null);
        });
    }
  }

  close(): void {
    this.active = false;
    this.modal.hide();
  }
}
